<?php

namespace app\supplier\controller;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use app\erp\model\Orderdb;
use Darabonba\OpenApi\Models\Config;
use PHPMailer\PHPMailer\PHPMailer;
use think\Controller;
use app\erp\model\Memberdb;


class Base extends Controller {
	public function _initialize() {
		define ( 'SITE_ID', 117 );
		define ( 'PARTNER_ID', 25 );
		$request = request ();
		$action = $request->action ();
		if ($action != 'songhuodan' && $action != 'saleht' && $action != 'caigouht') {
			if (! session ( 'supplier_id' )) {
				$this->redirect ( 'Login/index' );
			}
			define ( 'MEMBER_ID', session ( 'supplier_id' ) );	
		} else {
			$param = $request->param ();
			define ( 'MEMBER_ID', $param['member_id'] );
		}
		$member = new Memberdb ();
		$get_member = $member->memberInfo ( MEMBER_ID );
		$this->assign ( 'member_info', $get_member );
	}
	public function __system() {
		return true;
	}
	protected function ajaxReturn($data, $json_option = 0) {
		header ( 'Content-Type:application/json; charset=utf-8' );
		exit ( json_encode ( $data, $json_option ) );
	}
	protected function layuiReturn($data, $count = 0, $msg = '', $code = 0) {
		$return ['data'] = $data;
		$return ['count'] = $count;
		$return ['msg'] = $msg;
		$return ['code'] = $code;
		$this->ajaxReturn ( $return );
	}
	public function getSheetValue($sheet, $index) {
		$value = $sheet->getCell ( $index )->getValue ();
		if (is_object ( $value )) {
			$value = $value->__toString ();
		}
		return $value;
	}

    /**
     * Title("发送邮件")
     */
    public function sendMail()
    {
        $request = request();
        $param = $request->param();
        // 同步邮件地址
        $orderdb = new Orderdb();
        $get_order = $orderdb->where('order_no', $param['order_no'])->find();
        if ($get_order) {
            $temp = json_decode($get_order['api_param'], true);
            $temp ['email'] = $param['to_mail'];
            $orderdb->where('order_id', $get_order['order_id'])->update(array('api_param' => json_encode($temp)));
        }

        $to = $param['to_mail'];
        $url = $request->server('DOCUMENT_ROOT') . str_replace('http://tailongji.dixintong.com', '', $param['file_url']);
        $url = str_replace('https://tailongji.dixintong.com', '', $url);
        $mail = new PHPMailer();
        $mail->isSMTP();
        $mail->CharSet = 'utf8';
        $mail->Host = 'smtp.263.net';
        $mail->SMTPAuth = true;
        $mail->Username = 'limengqi@dixintong.com';
        $mail->Password = 'f9fB3fEAE6B4B6C9';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;
        $mail->setFrom('limengqi@dixintong.com', '泰龙吉');  //定义邮件及标题
        $mail->addAddress($to);  //要发送的地址和设置地址的昵称
        $mail->addReplyTo('limengqi@dixintong.com', '泰龙吉');
        $mail->addAttachment($url);
        $mail->Subject = $param['order_no'] . "--泰龙吉";  //添加该邮件的主题
        $mail->Body = '详见附件'; //该邮件内容
        return $mail->send();
        return true;
    }

    /**
     * Title("发送邮件附件")
     */
    public function sendMailTpl($email, $url, $title,$attr_name)
    {
        $mail = new PHPMailer();
        $mail->isSMTP();
        $mail->CharSet = 'utf8';
        $mail->Host = 'smtp.263.net';
        $mail->SMTPAuth = true;
        $mail->Username = 'limengqi@dixintong.com';
        $mail->Password = 'f9fB3fEAE6B4B6C9';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;
        $mail->isHTML(true);
        $mail->setFrom('limengqi@dixintong.com', '泰龙吉');  //定义邮件及标题
        $mail->addAddress($email);  //要发送的地址和设置地址的昵称
        $mail->addReplyTo('limengqi@dixintong.com', '泰龙吉');
        $mail->Subject = $title;  //添加该邮件的主题
        $mail->Body = '详见附件'; //该邮件内容
        $mail->addAttachment($url,$attr_name);
        return $mail->send();
    }
	
}